package com.totoro.springboot.dynamic_load.controller;

import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Niki on 2018/5/8 13:51
 */
@RestController
public class RunController {

    @RequestMapping("/restart/tomcat")
    public String restartTomcat() {
        System.out.println("重启Tomcat");
        try {
            Map<String,String> envs = System.getenv();
            List<String> envList = new ArrayList<>();
            for (Map.Entry<String, String> env : envs.entrySet()) {
                envList.add(String.format("%s=%s", env.getKey(), env.getValue()));
            }
            envList.add("CATALINA_HOME=D:\\server\\apache-tomcat-8.5.16");

            Process process = Runtime.getRuntime().exec("cmd /c D:\\server\\apache-tomcat-8.5.16\\bin\\shutdown.bat & D:\\server\\apache-tomcat-8.5.16\\bin\\startup.bat", envList.toArray(new String[envList.size()]));
            LineNumberReader reader = new LineNumberReader(new InputStreamReader(process.getInputStream()));
            String line = null ;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "success";
    }

//    @GetMapping("/root")
    public String getRootPath(HttpServletRequest request) {
        System.out.println(request.getContextPath());
        System.out.println(request.getLocalAddr());
        System.out.println(request.getLocale());
        System.out.println(request.getServletPath());
        System.out.println(request.getServerName());
        System.out.println(request.getServletContext().getRealPath("/"));

        return request.getServletPath();
    }

    @GetMapping("/newwar")
    public String newWar(HttpServletRequest request) throws IOException {
        String appPath = request.getServletContext().getRealPath("/");
        String[] paths = appPath.split("webapps");
        String appwarPath = paths[0] + File.separator + "webapps" + File.separator + "dynamic_load-0.0.1.war";
        File file = new File(appwarPath);
        if(!file.exists()){
            file.createNewFile();
        }
        FileOutputStream outputStream = new FileOutputStream(file);
        FileInputStream inputStream = new FileInputStream(new File("E:\\fsh_workspace\\code\\springboot\\dynamic_load\\target\\dynamic_load-0.0.1.war"));
        IOUtils.copyLarge(inputStream, outputStream);
        IOUtils.closeQuietly(outputStream);
        IOUtils.closeQuietly(inputStream);
        return appwarPath;
    }

    public static void main(String[] args) {
        String path = "D:\\server\\apache-tomcat-8.5.16\\webapps\\dynamic_load-0.0.1\\";
        String[] paths = path.split("webapps\\\\");
        System.out.println(paths[0]);
        System.out.println(paths[1]);
    }
}
